package cn.myframe.redis.msg;

import java.util.concurrent.CountDownLatch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisMsgListener2 {
	@Autowired
	Receiver receiver;
    @Autowired
    HelloReceiver helloReceiver;

	/**
	 * 监听过期记录
	 * @param connectionFactory
	 * @param listenerAdapter
	 * @return
	 */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("__keyevent@0__:expired"));
        return container;
    }
    
    /**
     * 监听方法
     * @return
     */
    @Bean(name="listenerAdapter")
    MessageListenerAdapter listenerAdapter() {
    	//回调数据处理方法
        return new MessageListenerAdapter(receiver, "dealUser");
    }

    @Resource
    RedisConnectionFactory connectionFactory;
    /**
	 * 监听参与记录
	 * @param connectionFactory
	 * @param listenerAdapter
	 * @return
	 */
    @Bean
    RedisMessageListenerContainer scontainer(
            MessageListenerAdapter slistenerAdapter) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(slistenerAdapter, new PatternTopic("dealUser"));
        return container;
    }
    
    /**
     * 监听方法
     * @return
     */
    @Bean(name="slistenerAdapter")
    MessageListenerAdapter slistenerAdapter() {
    	//回调数据处理方法
        return new MessageListenerAdapter(helloReceiver, "print");
    }
    

    @Bean(name="latch")
    CountDownLatch latch() {
        return new CountDownLatch(1);
    }

  

}
